<template>
    <div class="loginStaff">
        <el-form :rules="rules"
                 v-loading="loading"
                 element-loading-text="正在登录..."
                 element-loading-spinner="el-icon-loading"
                 element-loading-background="rgba(0, 0, 0, 0.8)"
                 ref="form"
                 :model="LoginForm"
                 class="loginContainerStaff">
            <h3 class="loginTile">员工系统</h3>
            <el-form-item prop="username">
                <el-input prefix-icon="element-icons el-icon202yonghu_yonghu3" v-model="LoginForm.username" placeholder="请输入用户名" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input prefix-icon="element-icons el-iconsuoding" v-model="LoginForm.password" placeholder="请输入密码" type="password" auto-complete="false"></el-input>
            </el-form-item>
            <el-form-item prop="code">
                <el-input prefix-icon="element-icons el-iconduigou" v-model="LoginForm.code" placeholder="点击更换验证码" type="text" auto-complete="false" style="width: 250px;margin-right: 5px"></el-input>
                <img :src="captchaUrl" @click="updateCaptcha">
            </el-form-item>
            <el-checkbox v-model="checked" class="loginRemeberMe">记住我</el-checkbox>
            <el-button type="primary" style="width: 100%" @click="submitLogin('form')">登录</el-button>
        </el-form>
    </div>
</template>

<script>



    export default {
        name: "Stafflogin",
        data(){
            return{
                //加time确保验证码可以刷新 不然可能只会返回一张验证码
                captchaUrl:'/api/login/captcha?time='+new Date(),
                LoginForm:{
                    username:'201902150751',
                    password:'123456',
                    code:'',
                },
                checked:true,
                rules:{
                    username:[{required:true,message:'请输入用户名',trigger:'blur'}],
                    password:[{required:true,message:'请输入密码',trigger:'blur'}],
                    code:[{required:true,message:'请输入验证码',trigger:'blur'}]
                },
                loading:false,
                urlparam:{
                    loginName:'',
                    password:'',
                    code:''
                }
            }
        },
        methods:{
            submitLogin(formName){
                this.urlparam.loginName=this.LoginForm.username;
                this.urlparam.password=this.LoginForm.password;
                this.urlparam.code=this.LoginForm.code;
                let pa={
                    id:'',
                    password:'',
                    code:''
                };
                pa.id=this.urlparam.loginName;
                pa.password=this.urlparam.password;
                pa.code=this.urlparam.code;
                this.$http({
                    url:'/login/driver',
                    method:'get',
                    params:pa
                }).then(result=>{
                    if(result.data.id!=null){
                        this.$message({
                            type:'success',
                            message:'登录成功',
                            showClose: true
                        });
                        window.sessionStorage.setItem("username",result.data.id);
                        window.sessionStorage.setItem("busId",result.data.busId);
                        window.sessionStorage.setItem("id",result.data.id);
                        this.$router.push("/StaffHello")
                    }else if(result.data==null){
                        this.$message({
                            type:'error',
                            message:'密码错误或验证码错误',
                            showClose: true
                        })
                    }else {
                        this.$message({
                            type:'error',
                            message:'未知错误',
                            showClose: true
                        })
                    }
                    console.log(result);
                }).catch((err)=>{
                    this.$message({
                        type:'error',
                        message:'验证码或者密码错误',
                        showClose:true
                    })
                })
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            updateCaptcha(){
                this.captchaUrl='/api/login/captcha?time='+new Date();
            }
        }
    }
</script>

<style>
    .loginContainerStaff{
        border-radius: 15px;
        background-clip: padding-box;
        margin: 188px auto;
        width: 320px;
        padding: 15px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
    }
    .loginTile{
        margin: 0px auto 40px auto;
        text-align: center;
    }
    .loginRemeberMe{
        text-align: left;
        margin: 0px 0px 15px 0px;
    }
    .el-form-item__content{
        display: flex;
        align-items: center;
    }
    .loginStaff{
        position: absolute;
        width: 100%;
        height: 100%;
        background:url(../assets/img/background2.png) no-repeat;
        background-size:100% 100%;
    }
</style>
